<?php
/**
 * Created by PhpStorm.
 * User: zhangrxiang
 * Date: 2016/11/9
 * Time: 9:20
 * Project name: PHPTEST
 * File name: 1.php
 * @copyright Copyright (c) 2016 ZRX Software LLC
 * @license https://github.com/zhangrxiang
 * @since 1.0.0
 */


include_once 'fun.php';

br("array_rand() 返回数组中的随机键名，或者如果指定函数返回键名不只一个，则返回一个包含随机键名的数组。");
$a=array("red","green","blue","yellow","brown",2,3,4,5,6,[]);
$a=['one'=>'one','two'=>'two','color'=>'red','sex'=>'nan'];
$random_keys=array_rand($a,3);
br($random_keys);
br();




br("array_push() 函数向数组尾部插入一个或多个元素。");
$a=array("red","green");
br(array_push($a,"blue","yellow",[[[]]]));
br($a);
br();

br("array_product() 函数计算并返回数组的乘积。");

$a=array(5,5,5,5);
br(array_product($a));

br();
br("array_pop() 函数删除数组中的最后一个元素。");
$a=array("red","green");
br(array_pop($a));
br($a);

br();
br("array_pad() 函数将指定数量的带有指定值的元素插入到数组中。");
$a=array("red","green");
br(array_pad($a,5,"blue"));
br();

br("<i style='color: red;margin: 0px 200px;'>fuck</i>");
br("array_multisort() 函数返回一个排序数组。您可以输入一个或多个数组。");
br("函数先对第一个数组进行排序，接着是其他数组，如果两个或多个值相同，它将对下一个数组进行排序。");
$a=array("Dog","Cat","Horse","Bear","Zebra");
$a2=array("a1Dog","c3Cat","g5gHorse","m6Bear","q8qZebra");
array_multisort($a,SORT_ASC,$a2,SORT_ASC);
br($a);
br($a2);

br();
br("array_merge_recursive() 函数用于把一个或多个数组合并为一个数组。");
$a1=array("a"=>"red","b"=>"green");
$a2=array("c"=>"blue","b"=>"yellow");
br(array_merge_recursive($a1,$a2));

br();
br("array_merge() 函数用于把一个或多个数组合并为一个数组。");
$a1=array("a"=>"red","b"=>"green");
$a2=array("c"=>"blue","b"=>"yellow");
br(array_merge($a1,$a2));

br();


br("array_map() 函数将用户自定义函数作用到数组中的每个值上，并返回用户自定义函数作用后的带有新的值的数组。");
function myfunction($v)
{
    return(md5($v));
}

$a=array(1,2,3,4,5);
br(array_map("myfunction",$a));


br();
br("array_keys() 函数返回包含数组中所有键名的一个新数组。");
$a=array("a" => "aa",1=>'tset',"Volvo"=>"XC90","BMW"=>"X5","Toyota"=>"Highlander");
br(array_keys($a,'aa',true));
br(array_keys($a));



br();
$a1 = array("a" => "red", "b" => "green", "c" => "blue1");
if (array_key_exists("a", $a1)):
    echo 111;
endif;


br();

br("array_intersect_key() 函数用于比较两个（或更多个）数组的键名 ，并返回交集。");
$a1=array("a"=>"red","b"=>"green","c"=>"blue1");
$a2=array("a"=>"red","c"=>"blue","d"=>"pink");

$result=array_intersect_key($a1,$a2);
br($result);
br();

br("array_intersect_assoc() 函数用于比较两个（或更多个）数组的键名和键值，并返回交集。");
$a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
$a2=array("a"=>"red","b"=>"green","c"=>"blue");
$result=array_intersect_assoc($a1,$a2);
br($result);
br();

br("array_intersect() 函数用于比较两个（或更多个）数组的键值，并返回交集。");
$a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow",'qqqq'=>'fuck');
$a2=array("e"=>"red","f"=>"green","g"=>"blue",'h'=>'fuck');

$result=array_intersect($a1,$a2);
br($result);
br();


br("array_flip() 函数用于反转/交换数组中的键名和对应关联的键值。");
$a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
$result=array_flip($a1);

br($result);
br();

br("array_filter() 函数用回调函数过滤数组中的元素。");
//
function test_odd($var)
{
    return $var & true;
//    return($var & true);
}

$a1=array("a","b",2,3,4,array(11,22,33));
br(array_filter($a1,"test_odd"));
br();

br("array_fill_keys() 函数用给定的指定键名的键值填充数组。");
$keys=array("a","b","c","d");
$a1=array_fill_keys($keys,"blue");
br($a1);
br();

br("用给定的键值填充数组：array_fill");

$a1=array_fill(3,4,"blue");
$a2=array_fill("1",5,"1111");
br($a1);
br($a2);
br();
br("比较两个数组的键名（使用用户自定义函数比较键名），并返回差集：");
function myfunction_array_diff_ukey($a,$b)
{
    if ($a===$b)
    {
        return 0;
    }
//    echo "a->".$a."<br>";
//    echo "b->".$b."<br>";
    return ($a>$b)?1:-1;
}

$a1=array("a"=>"red","b"=>"green","c"=>"blue");
$a2=array("a"=>"blue","b"=>"black","e"=>"blue","f"=>"ffblue");

$result=array_diff_ukey($a1,$a2,"myfunction_array_diff_ukey");
br($result);
br();

br("比较两个数组的键名和键值（并返回差集：array_diff_uassoc(array1,array2,array3...,myfunction);");
function myfunction_array_diff_uassoc($a,$b)
{
    if ($a===$b)
    {
        return 0;
    }
    return ($a>$b)?1:-1;
}

$a1=array("a"=>"red","b"=>"green","c"=>"blue");
$a2=array("d"=>"red","b"=>"green","e"=>"blue");
br($a1);
br($a2);
$result=array_diff_uassoc($a1,$a2,"myfunction_array_diff_uassoc");
br($result);
br(array_diff_assoc($a1,$a2));


br();

br("比较两个数组的键名，并返回差集：array_diff_key");

$a1=array("a"=>"red","b"=>"green","c"=>"blue");
$a2=array("a"=>"red","c"=>"blue","d"=>"pink");
br($a1);
br($a2);
$result=array_diff_key($a1,$a2);
br($result);
br();

br("比较两个数组的键名和键值，并返回差集：array_diff_assoc(array1,array2,array3...);");
$a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
$a2=array("a"=>"red","b"=>"green1","c"=>"blue");
br($a1);
br($a2);
$result=array_diff_assoc($a1,$a2);
br($result);

br();


br("array_diff() 函数用于比较两个（或更多个）数组的键值，并返回差集。array_diff");
$a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
$a2=array("e"=>"red","f"=>"green","g"=>"blue");
br($a1);
br($a2);
$result=array_diff($a1,$a2);
br($result);
br();


br("array_count_values() 函数用于统计数组中所有值出现的次数:array_count_values(array)");
$a=array("A","Cat","Dog","A","Dog");
br($a);
br(array_count_values($a));
br();


br("通过合并两个数组来创建一个新数组，一个元素为键名，另一个元素为键值：array_combine(keys,values);");
$fname=array("Peter","Ben","Joe");
$age=array("35","37","43");
$c=array_combine($fname,$age);
br($fname);
br($age);
br($c);
br();

br("把数组分割为带有两个元素的数组块：array_chunk(array,size,preserve_keys); ");
$cars=array("Volvo","BMW","Toyota","Honda","Mercedes","Opel");
br($cars);
br(array_chunk($cars,2));


br();//将数组的所有的键转换为大写或小写字母：
br("将数组的所有的键转换为大写或小写字母:array_change_key_case");
$age = array("Peter" => "35", "Ben" => "37", "Joe" => "43");
br(array_change_key_case($age, CASE_UPPER));
br();
br(array_change_key_case($age, CASE_LOWER));

